package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.annotation.SimpleValid;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.dto.ExchangeInputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.ExchangeService;
import com.partners.utils.ApiResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 兑换管理
 * @author liangyongle
 * @date 2018-12-28
 */
@RequestMapping("exchange")
@RestController
public class ExchangeController {

    @Resource
    private ExchangeService exchangeService;


    /**
     *管理员-获取所有兑换列表
     * @param  paging Paging
     * return list
    * */
    @PostMapping("list")
    public ApiResult getExchangeList(@BeanValid @RequestBody Paging paging,
                                    @SimpleParam("orderNo") Integer orderNo){
       Map<String,Object> list = exchangeService.getExchangeList(paging,orderNo);
      return ApiResultUtil.ok(list);
    }

    /**
     *用户-获取用户的兑换列表
     * @param  paging Paging uid Integer
     * return list
     * */
    @PostMapping("user_exchange")
    public ApiResult getUserExchange(@BeanValid @RequestBody Paging paging,
                                     @SimpleParam("uid") Integer uid){
        Map<String,Object> list = exchangeService.getUserExchangeList(paging,uid);
        return ApiResultUtil.ok(list);
    }

    /**
     *选择产品兑换
     * @param exchangeInputDTO ExchangeInputDTO
     * return Integer
     * */
    @PostMapping("exchange")
    public ApiResult exchangeProduct(@BeanValid @RequestBody ExchangeInputDTO exchangeInputDTO){
        if(exchangeInputDTO.getType() == 1 &&  exchangeInputDTO.getAddress() == null) {
            throw BusinessException.of(ErrorMessageEnum.ADDRESS_NOT_FOUND);
        }
        Integer result = exchangeService.exchangeProduct(exchangeInputDTO);
        return ApiResultUtil.ok(result);
    }



    /**
     * 收货
     * @Param id Integer
     * return Boolean
     * */
    @PostMapping("finishExchange")
    public ApiResult finishExchange(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        exchangeService.finishExchange(id);
        return ApiResultUtil.ok(SuccessMessageEnum.OPERATION_SUCCESS);
    }





    @PostMapping("test")
    public ApiResult test(@SimpleValid @NotNull @SimpleParam("id") Integer id){
        return ApiResultUtil.ok(id);
    }



}
